# -*- coding:utf-8 -*-
# time: 2025/7/28 ♡  15:44
# author:张伟
import random
import time
from datetime import datetime, timedelta

from models.post import BoardModel, PostModel
from exts import db
from models.auth import UserModel, RoleModel, Permission



def init_board():
    board_names = ["python", "flask", "Django", "爬虫", "前端"]
    for index, board_name in enumerate(board_names):
        board = BoardModel(name=board_name, priority=len(board_names) - index)
        db.session.add(board)
    db.session.commit()


def init_post():
    author = UserModel.query.first()
    for i in range(1, 200):
        title = f"第{i}篇帖子"
        content = f"这是第{i}篇帖子内容"
        board_id = i // 50 + 1
        likes_num = random.randint(0, 200)
        post = PostModel(title=title, content=content, board_id=board_id, author=author, likes_num=likes_num)
        db.session.add(post)
        db.session.commit()
        print("正在初始化第{}篇帖子".format(i))
        time.sleep(1)
    print("帖子表数据初始化成功")



def init_seven_day_post():
    now = datetime.now()
    seven_day_ago = now - timedelta(days=6, hours=now.hour, minutes=now.minute, seconds=now.second,
                                  microseconds=now.microsecond)
    author = UserModel.query.first()
    for i in range(7):
        for j in range(1, 51):
            date = seven_day_ago + timedelta(days=i, hours=random.randint(0, 23), minutes=random.randint(0, 59),)
            title = f"{date}发布的第{j}篇帖子"
            content = f"{date}发布的第{j}篇帖子内容"
            board_id = random.randint(1, 5)
            likes_num = random.randint(0, 200)
            post = PostModel(title=title, content=content, create_time=date, board_id=board_id, author=author, likes_num=likes_num)
            db.session.add(post)
            db.session.commit()
            print("正在初始化第{}篇帖子".format(j))
            time.sleep(1)
    print("帖子表数据初始化成功")


def init_roles():
    # 运营
    operator_role = RoleModel(name="运营", describe="负责管理帖子,评论和用户",
                         permissions=Permission.POST | Permission.COMMENT | Permission.USER)
    # 管理员
    admin_role = RoleModel(name="管理员", describe="负责整个网站的管理",
                      permissions=Permission.POST | Permission.COMMENT | Permission.USER | Permission.STAFF)
    # 开发者（权限是最大的）
    developer_role = RoleModel(name="开发者", describe="负责网站的开发", permissions=Permission.ALL_PERMISSION)

    db.session.add_all([operator_role, admin_role, developer_role])
    db.session.commit()
    print("角色添加成功！")



def bind_roles():
    user1 = UserModel.query.get("5q7Z5nLkJcJRnmUikzLgdB")
    user2 = UserModel.query.get("aCNDD4gURt5sbBb4joJSNy")
    user3 = UserModel.query.get("MrjiD788tJVhegQL2z6yNR")

    role1 = RoleModel.query.filter_by(name="运营").first()
    role2 = RoleModel.query.filter_by(name="开发者").first()
    role3 = RoleModel.query.filter_by(name="管理员").first()

    user1.roles = role1
    user2.roles = role2
    user3.roles = role3

    db.session.commit()
    print("用户和角色绑定成功！")

def init_develpoer():
    role = RoleModel.query.filter_by(name="开发人员").first()
    user = UserModel(username="张三", email="2947142980@qq.com", password="123456", roles=role)
    db.session.add(user)
    db.session.commit()
    print("初始化开发人员成功！")
